<?php
namespace Home\Controller;
use Think\Controller;
class UcenterController extends Controller {
    //ThinkPHP的构造函数
    /*public function _initialize() {
        //自动加载的类
        new \Org\Autoload();
        if (session('member_id') == '') {
            echo "<script>alert('您没有登录，请您登录后再访问此页面');window.location.href='/user/login';</script>";
            exit;
        }
        $this->member_id = $this->member_info['member_id'];
        //echo $member_id;exit;
        //根据配置来开启Redis
        /*if(C('REDIS_OPEN')==1){
            //300秒
            S(array('type'=>'redis','host'=>'127.0.0.1','port'=>'6379','prefix'=>'index','expire'=>300));
        }*
    }*/

    //此处对获取的UID进行处理 同步登录的时候，进行数据表的操作 以后可优化为访问某端后再AJAX写入
    //此处已经自动登录
    public function uc_login(){
        if(I('get.safe_code')!='zhaojinyin'){
            echo 'error';exit;
        }
        $uid=I('get.uid');
        $username=I('get.username');
        //echo $uid;exit;
        //此处将UC端的用户写入本地数据表！uid username password 先可以登录，然后有完善资料页面 存在则不写 稍后完善 修改密码等，都可以同步到UC和各个端 更新uid,username,password 以member_id为准
        if($uid>0){
            //连接UC端
            $UC=M('Members','uc_','DB_CONFIG');
            $username=$UC->where('uid='.$uid)->getField('username');
            $password=$UC->where('uid='.$uid)->getField('password');
            $usertype=$UC->where('uid='.$uid)->getField('usertype');
            //echo '35'.$username.'<br />'.$password;exit;
            $Member=M('Member');
            $count=$Member->where('member_id='.$uid)->count();
            //echo $count;exit;
            //存在则更新用户名和密码等 否则不做处理
            if($count==1){
                $data['uc_username']=$username;
                $data['member_passwd']=$password;
                $data['uc_usertype']=$usertype;
                $Member->where('member_id='.$uid)->save($data);
                $member_email=$Member->where('member_id='.$uid)->getField('member_email');
                $member_mobile=$Member->where('member_id='.$uid)->getField('member_mobile');
                //echo $member_name;exit;
                cookie('member_id',$uid,3600);
                cookie('member_email',$member_email,3600);
                cookie('member_mobile',$member_mobile,3600);
            }
            cookie('uid',$uid,3600); //写入uid,供同步登录使用
            dump(cookie());exit;
        }
        /*if($username!=''){
            //连接UC端
            $UC=M('Members','uc_','DB_CONFIG');
            $uid=$UC->where("username='".$username."'")->getField('uid');
            $password=$UC->where('uid='.$uid)->getField('password');
            $usertype=$UC->where('uid='.$uid)->getField('usertype');
            //echo '35'.$username.'<br />'.$password;exit;
            $Member=M('Member');
            $count=$Member->where('member_id='.$uid)->count();
            //echo $count;exit;
            //存在则更新用户名和密码等 否则不做处理
            if($count==1){
                $data['uc_username']=$username;
                $data['member_passwd']=$password;
                $data['uc_usertype']=$usertype;
                $Member->where('member_id='.$uid)->save($data);
                $member_email=$Member->where('member_id='.$uid)->getField('member_email');
                $member_mobile=$Member->where('member_id='.$uid)->getField('member_mobile');
                //echo $member_name;exit;
                cookie('member_id',$uid,3600);
                cookie('member_email',$member_email,3600);
                cookie('member_mobile',$member_mobile,3600);
            }
            cookie('uid',$uid,3600); //写入uid,供同步登录使用
            dump(cookie());exit;
        }*/
        //echo $member_name;exit;
    }

    //此处对获取的UID进行处理 进行安全处理 加些验证码 传uid进来，可进行后续处理
    public function uc_logout(){
        if(I('get.safe_code')!='zhaojinyin'){
            echo 'error';exit;
        }
        $uid=I('get.uid');
        //$member_name=M('Member')->where('member_id='.$uid)->getField('member_name');
        //cookie('member_id',$uid,3600);
        //cookie('member_name',$member_name,3600);
        //echo $member_name;exit;
        session('member_id',null);
        session('member_name',null);
        cookie('uid',null);
        cookie('member_id',null);
        cookie('member_name',null);
    }

}